import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

import 'home_state.dart';

class HomeCubit extends Cubit<HomeState> {
  HomeCubit() : super(HomeState().init());

  void onScaleStart(ScaleStartDetails details) {
    state.previousScale = state.scale;
    emit(state.clone()..previousScale = state.previousScale);
  }

  void onScaleUpdate(ScaleUpdateDetails details) {
    state.scale = state.previousScale * details.scale;
    emit(state.clone()..scale = state.scale);
  }

  void showIOSDialog(BuildContext context) {
    void changeColor(Color color) {
      emit(state.clone()..currentColor = color);
    }

    showCupertinoDialog(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          content: Container(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CupertinoTextField(
                  placeholder: state.txt,
                  onChanged: (value) {
                    emit(state.clone()..txt = value);
                  },
                ),
                CupertinoButton(
                    child: const Text(
                      '选择颜色',
                      style: TextStyle(color: Colors.black),
                    ),
                    onPressed: () {
                      showColorPicker(context);
                    })
              ],
            ),
          ),
        );
      },
    );
  }

  void showColorPicker(BuildContext context) {
    void changeColor(Color color) {
      emit(state.clone()..currentColor = color);
    }

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          titlePadding: const EdgeInsets.all(0),
          contentPadding: const EdgeInsets.all(0),
          content: SingleChildScrollView(
            child: ColorPicker(
              pickerColor: state.currentColor,
              onColorChanged: changeColor,
              colorPickerWidth: 300,
              pickerAreaHeightPercent: 0.7,
              // enableAlpha: _enableAlpha,
              // labelTypes: _labelTypes,
              // displayThumbColor: _displayThumbColor,
              // paletteType: _paletteType,
              pickerAreaBorderRadius: const BorderRadius.only(
                topLeft: Radius.circular(2),
                topRight: Radius.circular(2),
              ),
              // hexInputBar: _displayHexInputBar,
              // colorHistory: widget.colorHistory,
              // onHistoryChanged: widget.onHistoryChanged,
            ),
          ),
        );
      },
    );
  }
}
